package com.zhou.utils;

import com.zhou.Properties.JwtProperties;
import com.zhou.entity.JwtChuanCan;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;
//Jwt加密类 邹
public class JwtUtils {

    public static String createJWT(JwtChuanCan jwtChuanCan,JwtProperties jwtProperties){
        // 对象转换为Map
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> Map = objectMapper.convertValue(jwtChuanCan, Map.class);

        //Jwts工具类
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, jwtProperties.getSignature())//HS256:指定签名算法  指定签名秘钥
                .setClaims(Map)//设置自定义部分 Payload
                .setExpiration(new Date(System.currentTimeMillis() + jwtProperties.getTimelong()))//设置有效期 一小时
                .compact();//流式操作结束
        System.out.println(jwt);
        return jwt;
    }

}
